import { isPrime } from "./isPrime";

export default class NumberTimer {
  constructor(duration = 500) {
    this.duration = duration;
    this.number = 1;
    this.onNumberCreated = null;
    this.intervalId = null;
  }

  start() {
    if (this.intervalId) return;

    this.intervalId = setInterval(() => {
      this.onNumberCreated &&
        this.onNumberCreated(this.number, isPrime(this.number));
      this.number++;
    }, this.duration);
  }

  stop() {
    this.intervalId && clearInterval(this.intervalId);
    this.intervalId = null;
  }
}
